library(tidyverse)
library(scales)

###SET WORKING DIRECTORY and LOAD FILES ----

long <- read.csv("b_data/2_long_raw.csv")


##### Cleaning key conjoint variables----



# Recoding profile characteristics as factors
long <- long %>% 
  mutate(Faces =
           case_when(Faces == 1 ~ "White man",
                     Faces == 2 ~ "White woman",
                     Faces == 3 ~ "Light man",
                     Faces == 4 ~ "Light woman",
                     Faces == 5 ~ "Medium man",
                     Faces == 6 ~ "Medium woman",
                     Faces == 7 ~ "Brown man",
                     Faces == 8 ~ "Brown woman",
                     Faces == 9 ~ "Black man",
                     Faces == 10 ~ "Black woman"),
         Faces = factor(Faces, levels = c("White man", "White woman", "Light man", "Light woman", 
                                 "Medium man", "Medium woman", "Brown man", 
                                 "Brown woman", "Black man", "Black woman"), 
               ordered = TRUE),
         Background = 
           case_when(Background == "1" ~ "Legal immigrant",
                     Background == "2" ~ "Undoc. immigrant",
                     Background == "3" ~ "Naturalized citizen",
                     Background == "4" ~ "US-born legal parents",
                     Background == "5" ~ "US-born undoc. parents",
                     Background == "6" ~ "US-born legal grandparents",
                     Background == "7" ~ "US-born undoc. grandparents"),
         Background = factor(Background, levels = c("Legal immigrant",
                                                    "Undoc. immigrant",
                                                    "Naturalized citizen",
                                                    "US-born legal parents",
                                                    "US-born undoc. parents",
                                                    "US-born legal grandparents",
                                                    "US-born undoc. grandparents")),
         Identifies = 
           case_when(Identifies == "1" ~ "American",
                     Identifies == "2" ~ "Latino",
                     Identifies == "3" ~ "Hispanic",
                     Identifies == "4" ~ "Mexican-American",
                     Identifies == "5" ~ "Mexican",
                     Identifies == "6" ~ "White",
                     Identifies == "7" ~ "Black"),
         Identifies = factor(Identifies, levels = c("American",
                                                    "Latino",
                                                    "Hispanic",
                                                    "Mexican-American",
                                                    "Mexican",
                                                    "White",
                                                    "Black")),
         Language = 
           case_when(Language == "1" ~ "Only English",
                     Language == "2" ~ "Bilingual",
                     Language == "3" ~ "Limited English",
                     Language == "4" ~ "No English"),
         Language = factor(Language, levels = c("Only English",
                                                "Bilingual",
                                                "Limited English",
                                                "No English"),
                           ordered = is.ordered(Language)),
         Education =
           case_when(Education == "1" ~ "High school dropout",
                     Education == "2" ~ "High school graduate",
                     Education == "3" ~ "Bachelor's degree",
                     Education == "4" ~ "Advanced/graduate degree"),
         Education = factor(Education, levels = c("High school dropout",
                                                  "High school graduate",
                                                  "Bachelor's degree",
                                                  "Advanced/graduate degree"),
                            ordered = is.ordered(Education)),
         Profession = 
           case_when(Profession == "1" ~ "Waiter",
                     Profession == "2" ~ "Janitor",
                     Profession == "3" ~ "Sales manager",
                     Profession == "4" ~ "Teacher",
                     Profession == "5" ~ "IT professional",
                     Profession == "6" ~ "Doctor",
                     Profession == "7" ~ "Small business owner"),
         Profession = factor(Profession, levels = c("Waiter", "Janitor",
                                                    "Sales manager", "Teacher",
                                                    "IT professional", "Doctor",
                                                    "Small business owner")),
         Religion = 
           case_when(Religion == "1" ~ "Presbyterian",
                     Religion == "2" ~ "Evangelical",
                     Religion == "3" ~ "Catholic",
                     Religion == "4" ~ "Not religious"),
         Religion = factor(Religion, levels = c("Presbyterian", "Evangelical",
                                                "Catholic", "Not religious")),
         Political =
           case_when(Political == "1" ~ "Democrat",
                     Political == "2" ~ "Republican",
                     Political == "3" ~ "Independent",
                     Political == "4" ~ "Not political"),
         Political = factor(Political, levels = c("Democrat", "Republican",
                                                  "Independent", "Not political")),
         Spouse = 
           case_when(Spouse == "1" ~ "Not married",
                     Spouse == "2" ~ "S. is American",
                     Spouse == "3" ~ "S. is Black",
                     Spouse == "4" ~ "S. is White",
                     Spouse == "5" ~ "S. is same as profile"),
         Spouse = factor(Spouse, levels = c("Not married",
                                            "S. is American",
                                            "S. is Black",
                                            "S. is White",
                                            "S. is same as profile")))
# cleaning American rating variable
long <- long %>% 
  mutate(rating = 
           case_when(rating == 1 ~ 1,
                     rating == 2 ~ 2,
                     rating == 3 ~ 3,
                     rating == 4 ~ 4,
                     rating == 5 ~ 5,
                     rating == 6 ~ 6,
                     rating == 7 ~ 7))

long$rating_r <- rescale(long$rating) #rescales American ratings from 0 to 1

#### SAVING DATA FOR ANALYSIS-----

write.csv(long, "b_data/3_long_data_for_analysis.csv", row.names=FALSE)

white_long <- subset(long, race=="White respondent")
write.csv(white_long, "b_data/4_white_data_for_analysis.csv", row.names=FALSE)

black_long <- subset(long, race=="Black respondent")
write.csv(black_long, "b_data/5_black_data_for_analysis.csv", row.names=FALSE)

### Clear enviroment ----
rm(list = ls())
